﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DAO_NguoiDung
    {
        #region 1. Inserting
        public static bool ThemNguoiDung(DTO_NguoiDung nd)
        {
            bool result = false;
            try
            {
                string sql;
                sql = @"INSERT INTO db_NguoiDung VALUES  ('" + nd.MaNguoiDung + "','" + nd.TenDangNhap + "','" + nd.MatKhau + "','" + nd.CapDo + "')";
                RunQuery.RunNonQuery(sql);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Delete
        public static bool XoaNguoiDung(DTO_NguoiDung nd)
        {
            bool result = false;
            try
            {
                string sql;
                sql = @"DELETE FROM db_NguoiDung WHERE MaNguoiDung = '" + nd.MaNguoiDung+@"'";
                RunQuery.RunNonQuery(sql);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Retrieving

        public static List<DTO_NguoiDung> LayTatCaNguoiDung()
        {
            List<DTO_NguoiDung> lnd = new List<DTO_NguoiDung>();
            try
            {
                string sql;
                sql = @"SELECT * FROM db_NguoiDung";
                SqlDataAdapter a = new SqlDataAdapter();
                a = RunQuery.RunQueryData(sql);
                DataTable dt = new DataTable();
                a.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {
                    DTO_NguoiDung nd = new DTO_NguoiDung();
                    nd.MaNguoiDung = dr[0].ToString();
                    nd.TenDangNhap = dr[1].ToString();
                    nd.MatKhau = dr[2].ToString();
                    nd.CapDo = dr[3].ToString();
                    lnd.Add(nd);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lnd;
        }
        #endregion
    }
}
